import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import author from "../views/WechatYiYuan/author";
import wechatYiyuan from '../views/WechatYiYuan/router'
import axios from "axios"
import WechatAddress from "../views/AddressList/router";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'home',
        component: Home
    },
    {
        path: '/author',
        name: 'home',
        component: author
    },
    ...wechatYiyuan,
    ...WechatAddress
]

const router = new VueRouter({
    routes,
})
// 全局路由守卫
router.beforeEach((to, from, next) => {
    //  第一次进入项目
    if(!to.path.includes("vuewechat")){
        next();
    }
    let token = window.localStorage.getItem("user_token");
    if (!token && to.path != "/author" && to.meta.toweinxin == undefined) {
        window.localStorage.setItem("beforeLoginUrl", to.path); // 保存用户进入的url
        // snsapi_base  snsapi_userinfo 第一步用户授权，跳转目标页，在下一页得到code

        axios.get('/app/wechat/Main/wechat_info/mid/2.html').then(({data}) => {
            console.log(data)
            if (data.code === 0) {
                let AppId = data.data.appid; //公众号的唯一标识,开发者ID
                let urs = data.data.weixin_ip + data.data.vue_auth_url
                let redirect_urls = encodeURIComponent(urs);
                let urls = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + AppId + "&redirect_uri=" + redirect_urls + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
                // window.location.href = urls;
            }
        })
    } else {
        next();
    }
});
export default router
